package com.skywolf.chem.comments.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skywolf.chem.article.entity.Article;
import com.skywolf.chem.article.service.IArticleService;
import com.skywolf.chem.comments.entity.CommentInfo;
import com.skywolf.chem.comments.pojo.CommentQueryParam;
import com.skywolf.chem.comments.service.ICommentInfoService;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.usercenterapi.entity.CustomerInfo;
import com.skywolf.chem.usercenterapi.service.ICustomerInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 评论列表 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/ct/comments/comment-info")
public class CommentInfoController {

    @Autowired
    ICommentInfoService commentInfoService;

    @Autowired
    ICustomerInfoService customerInfoService;

    @Autowired
    IArticleService articleService;

    @ApiOperation(value = "发起评论",notes = "发起评论")
    @RequestMapping(value = "/comment",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> comment(HttpServletRequest request,
                                            @RequestBody @Validated CommentInfo commentInfo) {
        String uid = request.getHeader("user_id");
        CustomerInfo users = customerInfoService.getById(Long.valueOf(uid));

        if(commentInfo.getArticleId()==null){
            return RespData.failure(ResponseCodeEnum.COMMENT_ERROR);
        }
        if(commentInfo.getCommentDesc().length()>50 || StringUtils.isBlank(commentInfo.getCommentDesc())){
            return RespData.failure(ResponseCodeEnum.COMMENT_long_ERROR);
        }
        Article article = articleService.getById(commentInfo.getArticleId());
        if(article==null){
            return RespData.failure(ResponseCodeEnum.COMMENT_ERROR);
        }
        commentInfo.setCusNikeName(users.getNickName());
        commentInfo.setCusUid(users.getId());
        commentInfo.setCusHeadImg(users.getHeadImg());
        commentInfoService.save(commentInfo);
        return RespData.success("success");
    }

    @ApiOperation(value = "点赞评论",notes = "点赞评论")
    @RequestMapping(value = "/commentPraise",method = {RequestMethod.GET,RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> commentPraise(HttpServletRequest request,
                                          @RequestParam(value = "",required = true) String id) {
        CommentInfo info = commentInfoService.getById(id);
        info.setParise(info.getParise()+1);
        commentInfoService.updateById(info);
        return RespData.success(info.getParise());
    }

    @ApiOperation(value = "取消评论点赞",notes = "取消评论点赞")
    @RequestMapping(value = "/delPraise",method = {RequestMethod.GET,RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delPraise(HttpServletRequest request,
                                          @RequestParam(value = "",required = true) String id) {
        CommentInfo info = commentInfoService.getById(id);
        Integer count = info.getParise()-1;
        if(count<=0){
            count=0;
        }
        info.setParise(count);
        commentInfoService.updateById(info);
        return RespData.success(count);
    }

    @ApiOperation(value = "根据帖子id,查询其下的评论",notes = "根据帖子id,查询其下的评论")
    @RequestMapping(value = "/getCommentByArticleId",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<CommentInfo> getCommentByArticleId(HttpServletRequest request,
                                                   @RequestBody @Validated CommentQueryParam queryParam) {
        String uid = request.getHeader("user_id");
        IPage<CommentInfo> page = commentInfoService.getCommentList(queryParam,uid);
        PageVO<CommentInfo> pageVO = new PageVO<>(page);
        return RespData.success(pageVO);
    }

    @ApiOperation(value = "我的评论列表",notes = "我的评论列表")
    @RequestMapping(value = "/myCommentList",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<CommentInfo> myCommentList(HttpServletRequest request,@RequestBody @Validated CommentQueryParam queryParam) {
        String uid = request.getHeader("user_id");
        PageVO<CommentInfo> pageVO = commentInfoService.myCommentList(queryParam,Long.valueOf(uid));
        return RespData.success(pageVO);
    }

    @ApiOperation(value = "举报评论",notes = "举报评论")
    @RequestMapping(value = "/tipOff",method = {RequestMethod.GET,RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> tipOff(HttpServletRequest request,@RequestParam(value = "id",required = true) Long id) {
        return RespData.success("success");
    }
}
